package com.dingyh.shopwebbackground.controller;


import com.dingyh.shopwebbackground.entity.Address;
import com.dingyh.shopwebbackground.service.AddressService;
import com.dingyh.shopwebbackground.util.CommonResult;
import com.dingyh.shopwebbackground.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dingyuhan
 * @since 2023-02-16
 */
@RestController
@CrossOrigin
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressService addressService;

    @RequestMapping("/addAddress")
    public CommonResult<Object> addAddress(String userid, String tel, String province, String city, String area, String detail,String receiptname){
        Address address = Address.builder()
                .id(UUIDUtil.generator())
                .userid(userid)
                .tel(tel)
                .province(province)
                .city(city)
                .area(area)
                .detail(detail)
                .receiptname(receiptname)
                .build();
        boolean result = addressService.save(address);
        if (result) {
            return new CommonResult(200, "提交成功", null);
        } else {
            return new CommonResult(500, "提交失败", null);
        }
    }

    @RequestMapping("getAddressList")
    public CommonResult<Object> getAddressList(String userid){
        Map<String,Object> map = new HashMap<>();
        map.put("userid",userid);
        List<Address> addresses = addressService.getAddressList(map);
        return new CommonResult(200,"",addresses);
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @RequestMapping("removeAddress")
    public CommonResult<Object> removeAddress(String id){
        if(addressService.removeAddress(id)){
            return new CommonResult(200, "删除成功！", null);
        }else{
            return new CommonResult(500, "删除失败！", null);
        }
    }
}

